import random
import sys
import time

def print_welcome():
    print('---------------------------------------')
    print('|                                     |')
    print('|          人生重开模拟器             |')
    print('|                                     |')
    print('---------------------------------------')

def get_initial_attributes():
    while True:
        print('请输入你的初识属性：(总和不超过20，每项取值1-10)')
        face = int(input('请输入颜值：'))
        strong = int(input('请输入体质：'))
        iq = int(input('请输入智力：'))
        home = int(input('请输入家境：'))
        if all(1 <= attr <= 10 for attr in [face, strong, iq, home]) and (face + strong + iq + home == 20):
            return face, strong, iq, home
        else:
            print('属性输入错误，请重新输入')

def determine_gender():
    return 'boy' if random.randint(1, 100) % 2 == 1 else 'girl'

def determine_birth_conditions(home, face, strong, iq):
    point = random.randint(1, 3)
    if home == 10:
        print('你出生在帝都，你的父母是高官，你的家境非常好')
        return home + 1, face + 1, strong, iq + 1
    elif 7 <= home <= 9:
        if point == 1:
            print('你出生在大城市，你的父母是公务员，你的家境不错')
            return home, face + 2, strong, iq
        elif point == 2:
            print('你出生在大城市，你的父母是企业高管，你的家境不错')
            return home + 2, face, strong, iq
        else:
            print('你出生在大城市，你的父母是大学教授，你的家境不错')
            return home, face, strong, iq + 2
    elif 4 <= home <= 6:
        if point == 1:
            print('你出生在小城市，你的父母是医生，你的家境一般')
            return home, face, strong + 1, iq
        elif point == 2:
            print('你出生在小城市，你的父母是教师，你的家境一般')
            return home, face, strong, iq + 1
        else:
            print('你出生在小城市，你的父母是个体户，你的家境一般')
            return home + 1, face, strong, iq
    else:
        if point == 1:
            print('你出生在农村，你的父母是农民，你的家境非常差')
            return home, face - 2, strong + 1, iq
        elif point == 2:
            print('你出生在农村，你的父母是无业游民，你的家境非常差')
            return home - 1, face, strong, iq
        else:
            print('你出生在农村，你的父母感情不和，你的家境非常差')
            return home, face - 1, strong, iq

def childhood_events(age, gender, home, face, strong, iq):
    info = f'你今年{age}岁，'
    point = random.randint(1, 3)

    if gender == 'girl' and home <= 3 and point == 1:
        print(info + '你家里人重男轻女非常严重，你被遗弃了\n游戏结束')
        sys.exit(0)
    elif strong < 5 and point == 1:
        info += '你生病了，身体非常虚弱,'
        if home > 5:
            info += '但你的父母给你买了药，你很快恢复了'
            strong += 1
            home -= 1
        else:
            info += '你的父母没钱买药，你的身体越来越差'
            strong -= 1
    elif face < 5 and age >= 7:
        info += '你长相丑陋，没有人喜欢你'
        if iq > 5:
            info += '但你的智商高，最终有人喜欢你了'
            iq += 1
        else:
            if gender == 'boy':
                info += '你和小伙打架，被老师批评了'
                strong += 1
            else:
                info += '你经常被欺负，你很伤心'
                face -= 1
    elif iq < 5:
        info += '你智商低，常被同学嘲笑'
        if face > 5:
            info += '但你颜值高，最终有人喜欢你了'
            face += 1
        else:
            if gender == 'boy':
                info += '你和小伙打架，被老师批评了'
                strong += 1
            else:
                info += '你经常被欺负，你很伤心'
                face -= 1
    else:
        info += '你很健康，你的父母也喜欢你'
    
    print(info)
    time.sleep(0.5)
    return home, face, strong, iq

def adolescence_simulation(age, face, strong, iq, home):
    """模拟10-20岁的青春期事件"""
    info = f'你今年{age}岁，'
    if iq > 7 and age >= 15:
        info += '你在学校成绩优异，获得了奖学金。'
        iq += 1
    elif face > 7 and age >= 16:
        info += '你因为颜值出众，成为学校里的风云人物。'
    elif strong < 5 and age >= 13:
        info += '你在学校因为体质差，容易生病。'
        strong -= 1
    else:
        info += '你过着平凡的青春期生活。'

    print(info)
    time.sleep(0.5)
    return face, strong, iq, home

def adulthood_simulation(face, strong, iq, home):
    print('进入成年阶段...')
    for age in range(20, 61):
        info = f'你今年{age}岁，'
        if iq > 8 and strong > 8:
            info += '你事业有成，身体健康，过上了理想的生活。'
        elif home > 7:
            info += '你利用家族资源发展事业，达成了较高的成就。'
            home -= 1
        elif face > 7:
            info += '你以颜值吸引了许多人，生活充实。'
        elif strong < 5:
            info += '你身体较差，日常生活受到影响。'
            strong -= 1
        else:
            info += '你过着平凡的生活。'

        print(info)
        time.sleep(0.5)

def elderly_simulation(face, strong, iq, home):
    print('进入老年阶段...')
    for age in range(60, 81):
        info = f'你今年{age}岁，'
        if strong > 6:
            info += '你身体健康，能享受平静的晚年生活。'
            strong -= 1
        elif iq > 7:
            info += '你智力较高，晚年喜欢读书和下棋。'
        elif home > 5:
            info += '你的家境较好，晚年生活无忧。'
            home -= 1
        else:
            info += '你的身体状况较差，晚年生活平淡。'
        
        print(info)
        time.sleep(0.5)

def main():
    print_welcome()
    face, strong, iq, home = get_initial_attributes()
    print(f'你的初识属性为：颜值{face}，体质{strong}，智力{iq}，家境{home}')

    gender = determine_gender()
    print(f'你的性别为：{gender}')

    home, face, strong, iq = determine_birth_conditions(home, face, strong, iq)
    print(f'你的最终属性为：颜值{face}，体质{strong}，智力{iq}，家境{home}')

    print("进入幼年阶段...")
    for age in range(1, 11):
        home, face, strong, iq = childhood_events(age, gender, home, face, strong, iq)
    
    print("进入青春期阶段...")
    for age in range(10, 21):
        face, strong, iq, home = adolescence_simulation(age, face, strong, iq, home)

    adulthood_simulation(face, strong, iq, home)
    elderly_simulation(face, strong, iq, home)
    
    print("你的人生模拟结束了，谢谢体验！")

if __name__ == "__main__":
    main()
